package com.activities;

import java.io.File;

import android.app.Dialog;
import android.app.TabActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

import com.common.utils.file.TextFileManager;
import com.common.utils.filechoose.intents.Intents;
import com.preferences.MainPreference;

public class JavaPadActivity extends TabActivity {

	private TabHost tabHost;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		/* create tab view */
		tabHost = (TabHost) findViewById(android.R.id.tabhost);

		TabSpec defaultTabSpec = this.createNewEditTextTab(tabHost, null);
		tabHost.addTab(defaultTabSpec);

		tabHost.setCurrentTab(ActivitiesConstants.DEFAULT_CURRENT_TAB);
		/* */
	}

	@Override
	public boolean onCreateOptionsMenu(Menu mainMenu) {
		MenuInflater inflaterMenu = getMenuInflater();
		inflaterMenu.inflate(R.menu.main_menu, mainMenu);
		return true;
	}

	@Override
	public void onStart() {
		super.onStart();
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(this);
		String textFont = pref.getString(getString(R.string.font_key), "");
		if (textFont.equals("serif")) {
			((EditText) findViewById(tabHost.getCurrentTab() + 1)).setTypeface(Typeface.SERIF);
		} else if ((textFont.equals("default")) || (textFont.equals(""))) {
			((EditText) findViewById(tabHost.getCurrentTab() + 1)).setTypeface(Typeface.DEFAULT);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.id_new:
			TabHost.TabSpec newTab = createNewEditTextTab(this.tabHost, null);
			if (newTab != null) {
				tabHost.addTab(newTab);
			}
			break;
		case R.id.id_open:
			openFile();
			break;
		case R.id.id_config:
			Intent confIntent = new Intent(this, MainPreference.class);
			startActivity(confIntent);
			break;
		case R.id.id_save:
			break;
		case R.id.id_save_as:
			break;
		case R.id.id_exit:
			finish();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case ActivitiesConstants.REQUEST_OPEN:
			if ((resultCode == RESULT_OK) && (data != null)) {
				Uri fileUri = data.getData();
				if (fileUri != null) {
					String filePath = fileUri.getPath();
					File file = new File(filePath);
					if (file.exists()) {
						String content = TextFileManager.readTextFile(file);
						TabHost.TabSpec openedFileTab = this.createNewEditTextTab(tabHost, content);
						if(openedFileTab != null) {
							tabHost.addTab(openedFileTab);
						}
						//((EditText) findViewById(tabHost.getCurrentTab() + 1)).setText(content);
						Toast.makeText(getBaseContext(),
								"File " + file.getName() + " opened",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
			break;
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int Id) {
		Dialog dialog = null;
		switch(Id) {
		
		}
		return dialog;
	}

	public void openFile() {
		Intent openIntent = new Intent(Intents.OPEN_FILE_CHOOSER);
		openIntent.putExtra(Intents.FILE_CHOOSER_TITLE,
				getString(R.string.filechoose_open));
		openIntent.putExtra(Intents.FILE_CHOOSER_BUTTON,
				getString(R.string.filechooser_open_button));

		try {
			startActivityForResult(openIntent, ActivitiesConstants.REQUEST_OPEN);
		} catch (ActivityNotFoundException e) {
			// No compatible file manager was found.
			Toast.makeText(this, "Fail start activity", Toast.LENGTH_SHORT)
					.show();
		}
	}

	private TabHost.TabSpec createNewEditTextTab(TabHost host, final String text) {
		if (!(host.getTabWidget().getChildCount() >= ActivitiesConstants.TAB_COUNT)) {

			try {

				// generate next tab Id
				final int countTabs = host.getTabWidget().getTabCount() + 1;
				String strSpec = "tab" + countTabs;
				//

				// generate next tab Indicator
				String strInd = getString(R.string.def_document_name) + " "
						+ countTabs;
				//

				TabHost.TabSpec newTabSpec = tabHost.newTabSpec(strSpec);
				newTabSpec.setContent(new TabContentFactory() {

					@Override
					public View createTabContent(String tag) {
						EditText newEditText = new EditText(
								JavaPadActivity.this);
						newEditText.setId(countTabs);
						newEditText.setGravity(Gravity.TOP);
						newEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
						newEditText.setText(text);
						// TODO Auto-generated method stub
						return newEditText;
					}

				});
				newTabSpec.setIndicator(strInd);
				return newTabSpec;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}